package learning.numbers.pro;



import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
import android.os.Process;

public class GameThread extends Thread {

	
	// desired fps
	private final static int 	MAX_FPS = 25;
	// maximum number of frames to be skipped
	private final static int	MAX_FRAME_SKIPS = 5;
	// the frame period
	private final static int	FRAME_PERIOD = 1000 / MAX_FPS;	
	int i=0;
	//if the game is paused
	private boolean isPaused = false;
	
	
	private static final String TAG = GameThread.class.getSimpleName();
	private SurfaceHolder surfaceHolder;
	private ProGamePanel gamePanel;


	// flag to hold game state
	private boolean running= false;
	
	public GameThread(SurfaceHolder surfaceHolder, ProGamePanel gamePanel) {
	 super();
	 this.surfaceHolder = surfaceHolder;	 
	 this.gamePanel = gamePanel;
	}

 public void pauseGame(){	 
	 this.isPaused = true;
 }
 
 public void unpauseGame(){
	 this.isPaused = false;
 }
 
 public boolean isPaused(){
	 return this.isPaused;
 }
	

 public void setRunning(boolean running) {
  this.running = running;
 }
 
 

 @Override
 public void run() {
	  Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);	  
	  Canvas canvas;
	  Log.d(TAG, "Starting game loop");	  
	  long beginTime;		// the time when the cycle begun
	  long timeDiff;		// the time it took for the cycle to execute
	  int sleepTime;		// ms to sleep (<0 if we're behind)
	  int framesSkipped;	// number of frames being skipped 
	  
	  
	  sleepTime = 0;

	  //Game loop
	  while (running) {
	   canvas = null;
	   while (isPaused && running) { // need to check running  to avoid bugs when returning to the game with game paused
		   try {		             // 
		   Thread.sleep(100);
		   } catch (InterruptedException e) {
		   }
		   // coming out of pause, set the last tick to now.
		   beginTime = System.currentTimeMillis();
	}
	   
	   // try locking the canvas for exclusive pixel editing on the surface
	   try {
		canvas = this.surfaceHolder.lockCanvas();
	    //Log.d(TAG, "mainLoop");
	    synchronized (surfaceHolder) {
	    	if (!isPaused) {
			beginTime = System.currentTimeMillis();
			framesSkipped = 0;	// resetting the frames skipped
			// update game state
			this.gamePanel.update();
			
			// draws the canvas on the panel
			
			this.gamePanel.render(canvas);
			// calculate how long did the cycle take
			timeDiff = System.currentTimeMillis() - beginTime;
			// calculate sleep time
			sleepTime = (int)(FRAME_PERIOD - timeDiff);
	    	
			if (sleepTime > 0) {
				// if sleepTime > 0 we're OK
				try {
					// send the thread to sleep for a short period
					// very useful for battery saving
					Thread.sleep(sleepTime);
				} catch (InterruptedException e) {}
			}

			while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
				// we need to catch up
				// update without rendering
				this.gamePanel.update();
				// add frame period to check if in next frame
				sleepTime += FRAME_PERIOD;
				framesSkipped++;
			}
	    }
	    }
	   } finally {
	    // in case of an exception the surface is not left in
	    // an inconsistent state
	    if (canvas != null) {
	     surfaceHolder.unlockCanvasAndPost(canvas);
	    }
	   } 
	  }
	 }

}


